
package org.springblade.common.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {

	public static Integer getOneRandom(){
		Random rd = new Random();
		int r = rd.nextInt(1000);
		return r;
	}
	public static String getTimeString(){
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	public static Long getRandomLong(){
		String date =  new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		return Long.parseLong(date);
	}
	/**
	 * 分隔数组 根据段数分段 <多出部分在最后一个数组>
	 *
	 * @param data     被分隔的数组
	 * @param segments 需要分隔的段数
	 * @return
	 */
	public static List<List> subListBySegment(List data, int segments) {

		List<List> result = new ArrayList<>();

		int size = data.size();// 数据长度

		if (size > 0 && segments > 0) {// segments == 0 ，不需要分隔

			int count = size / segments;// 每段数量

			List cutList = null;// 每段List

			for (int i = 0; i < segments; i++) {
				if (i == segments - 1) {
					cutList = data.subList(count * i, size);
				} else {
					cutList = data.subList(count * i, count * (i + 1));
				}
				result.add(cutList);
			}
		} else {
			result.add(data);
		}
		return result;
	}

	/**
	 * 分隔数组 根据每段数量分段
	 *
	 * @param data  被分隔的数组
	 * @param count 每段数量
	 * @return
	 */
	public static List<List> subListByCount(List data, int count) {

		List<List> result = new ArrayList<>();

		int size = data.size();// 数据长度

		if (size > 0 && count > 0) {

			int segments = size / count;// 商
			/**
			 * 1.整除，  即分隔为segments     段
			 * 2.除不尽，即分隔为segments + 1 段
			 */
			segments = size % count == 0 ? segments : segments + 1; // 段数

			List cutList = null;// 每段List

			for (int i = 0; i < segments; i++) {
				if (i == segments - 1) {
					cutList = data.subList(count * i, size);
				} else {
					cutList = data.subList(count * i, count * (i + 1));
				}
				result.add(cutList);
			}
		} else {
			result.add(data);
		}
		return result;
	}
}
